<template>
    <div>
        <div class="account-safe">
            <div class="account-safe-title">手机</div>
            <div>未绑定</div>
        </div>
        <hr class="account-safe-hr"/>
        <div class="account-safe">
            <div class="account-safe-title">邮箱</div>
            <div>未绑定</div>
        </div>
        <hr class="account-safe-hr"/>
        <div class="account-safe">
            <div class="account-safe-title">密码</div>
            <div class="account-safe-inner" @click="handleChangePassword">修改</div>
        </div>


        <el-dialog title="修改密码"
                   width="500px"
                   :visible.sync="changePasswordVisible"
        >
            <el-form :model="formPassword" :rules="passRules" ref="passwordForm" >
                <el-form-item label="旧密码：" label-width="120px" prop="oldPassword">
                    <el-input v-model="formPassword.oldPassword"></el-input>
                </el-form-item>
                <el-form-item label="新密码：" label-width="120px" prop="newPassword">
                    <el-input v-model="formPassword.newPassword" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码：" label-width="120px" prop="confirmPassword">
                    <el-input v-model="formPassword.confirmPassword" show-password></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" @click="changePasswordVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="formRePassSubmit('passwordForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {changeUserPassword} from "../../api/authority";

    export default {
        name: "safe",
        data(){
            return {
                changePasswordVisible: false,
                formPassword: {
                    oldPassword: "",
                    newPassword: "",
                    confirmPassword: ""
                },
                passRules: {
                    oldPassword: [
                        { required: true, message: '请输入旧密码', trigger: 'blur' },
                    ],
                    newPassword: [
                        { required: true, message: '请输入新密码', trigger: 'blur' },
                        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
                        { validator: this.checkData, trigger: 'blur'}
                    ],
                    confirmPassword: [
                        { required: true, message: '请确认密码', trigger: 'blur' },
                        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
                        { validator: this.checkData, trigger: 'blur'}
                    ]
                }
            }
        },
        methods:{
            handleChangePassword(){
                this.changePasswordVisible = true;
                this.formPassword = {
                    oldPassword: "",
                    newPassword: "",
                    confirmPassword: ""
                }
            },
            formRePassSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.changePasswordVisible = false;
                        changeUserPassword({
                            oldPassword: this.formPassword.oldPassword,
                            newPassword: this.formPassword.newPassword,
                            confirmPassword: this.formPassword.confirmPassword
                        }).then((res) => {
                            if (res.data && res.data.code === 200 && res.data.success) {
                                this.$message.success("修改成功！请重新登录");
                                this.$emit('handleLogout');
                            } else {
                                this.$message.error("修改失败！")
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
